apiready = function() {
    seajs.use(['../../V', "../../api", "../../jquery", "../../app", "../../service", "../../dialog"], function(V, $api, $, $app, service, dialog) {
        var vm = {
            el: '.container',
            data: {
                personInfo: {
                    id: '',
                    nikename: '',
                    photo: '',
                    is_seller: ''
                },
                higherInfo: {}, //我的推荐人的信息
                unionid: '',
                is_shop: '',
                orderList: '',
                popOpenShop: true, //是否弹出开店邀请码
                bs: "", //ajax弹出服务器提示控制变量
                ShopRedTip:false
            },
            methods: {
                // 前往我的团队
                goMyMember: function() {
                    api.openWin({
                        name: 'myteam',
                        url: '../team/team.html'
                    });

                },

                // 进入商城
                intoShop: function(cb) {
                    var that = this;
                    console.log('person intoShop方法开始');
                    if ($api.getStorage("user")) {
                        this.bs = 1;
                    } else {
                        this.bs = "";
                    }
                    if (this.bs == "") { //游客模式，没有缓存
                        $('.batch_classfiy').css("display", "block");
                        return;
                    } else {
                        $app.ajax(
                            service['getUserShopInfo'], {
                                data: {
                                    values: {
                                        appid: $app.appid,
                                        userid: $api.getStorage("user").data.out_user_id,
                                        mask: 'none',
                                        bs: ''
                                    }
                                }
                            },
                            function(res) {
                                if (res.code == 0) {
                                    $api.setStorage("shop_unionid", res.data.shop_unionid);
                                    that.unionid = $api.getStorage("shop_unionid");
                                    $api.setStorage("shop_data", res.data);
                                    $api.setStorage("user", res);
                                    cb();
                                }
                            })
                    }

                },
                toSet: function() {
                    api.sendEvent({
                        name: 'preload_user'
                    })
                    $app.openWin("setUser",{},{
                      reload:true
                    });
                },
                init: function(et) {
                    this.personInfo = $api.getStorage("user").data;
                    console.log(JSON.stringify(this.personInfo))
                    this.higherInfo = this.personInfo.higher;

                    this.getList();
                    this.getShopRedTip()
                },
                getList: function(et) {
                    var self = this;
                    $app.ajax(service.personalCenterStatistics, {
                        data: {
                            values: {
                                unionid: $api.getStorage("shop_unionid"),
                                mask:"none"
                            }
                        }
                    }, function(res) {
                        console.log(JSON.stringify($api.getStorage('shop_data')));
                        if (res.code == 0) {
                            self.is_shop = $api.getStorage("shop_data").is_seller;
                            self.orderList = res.data;
                            self.popOpenShop = $api.getStorage("shop_data").is_store == 0 ? true : false; //弹出开店邀请码
                        } else {
                            console.log(res.message || res.msg);
                        }
                    });
                },
                classfiySure: function() {
                    api.closeToWin({
                        name: 'WeChat_login'
                    });
                },
                shop_get: function() {
                    $app.ajax(
                        service.shopDetail, {
                            data: {
                                values: {
                                    appid: $app.appId,
                                    userid: this.personInfo.out_user_id,
                                    mask:"none"
                                }
                            }
                        },
                        function(ret) {
                            console.log('要根据店铺是否有地址信息跳转页面（没有店铺地址就跳转到编辑店铺页面让他填写店铺地址，有的话就直接跳到店铺中心）')
                            console.log(JSON.stringify(ret))
                            if (ret.data.shop_location == "") {
                                api.openWin({
                                    name: 'shopCard',
                                    pageParam: {
                                        shopId: ret.data.shop_code
                                    },
                                    url: '../shop_card/shop_card.html',
                                    reload: true
                                });
                            } else {
                                api.openWin({
                                    name: 'my_shop',
                                    url: '../my_shop/my_shop.html',
                                    reload: true
                                });
                            }

                        })
                },
                //--去推荐人的店铺
                goRecommend: function(et) {
                    var out_user_id = '';
                    if (this.higherInfo.length > 0 || JSON.stringify(this.higherInfo)!='{}') {
                        var out_user_id = this.higherInfo.out_user_id
                    }
                    api.openWin({
                        name: 'single_shop',
                        url: "../shop_index/single_shop.html",
                        pageParam: {
                            out_user_id: out_user_id
                        },
                        reload: true
                    });
                },
                //--我的地址
                goMyAddress: function(et) {
                    $app.openWin("buyAddress");
                },
                //--我的优惠券
                goMyCoupon: function(et) {
                    $app.openWin("buyMyCoupon", "");
                },
                //--我的收藏夹
                goMyCollect: function(et) {
                    $app.openWin("buyMyCollect");
                },
                //--我的足迹
                goMyfootprint: function(et) {
                    $app.openWin("buyMyFootprint");
                },
                //--我的评价
                goMyComment: function(et) {
                    $app.openWin("buyMyComment");
                },
                //--添加会员商品
                myShopAddMemberGoods: function(et) {
                    var that = this;
                    $app.ajax(service['getGoodsList'], {
                        data: {
                            values: {
                                shop_code: $api.getStorage('user').data.shop_code,
                                is_member_product: 1,
                            }
                        }
                    }, function(res) {
                      if(res.code==0){
                        $app.openWin('shopEditMember', {
                            shopId: $api.getStorage('user').data.shop_code
                        })
                      }
                    });
                },
                //--我的订单，全部
                goMyorderMore: function(et) {
                    $app.openWin("buyOrderMy", {
                        index: 0
                    });
                },
                //--我的客服
                goMyService: function() {
                    $app.openWin("buyMyService");
                },
                getQRCode: function() {
                  if($api.getStorage('user').data.star==0){
                    api.toast({
                        msg: '您还没有自己店铺',
                        duration: 2000,
                        location: 'bottom'
                    });
                    return
                  }
                  var photoBrowser = api.require('photoBrowser');
                  var that = this
                    $app.ajax(
                        service.createPict, {
                            data: {
                                values: {
                                    appid: $app.appId,
                                    mask:"none"
                                }
                            }
                        },
                        function(res) {
                            if (res.code==0) {
                              photoBrowser.open({
                                  images: [res.data],
                                  bgColor: '#000'
                              }, function(ret, err) {
                                  if (ret.eventType == 'click') {
                                      photoBrowser.close();
                                  }
                                  if (ret.eventType == 'longPress') {
                                      that.upFile(res.data, function() {
                                          app.toast('已下载至手机相册')
                                      })
                                  }
                              });
                            }else{
                              if(res.code==559){//没有上传会员商品
                                setTimeout(function(){
                                  $app.openWin('shopEditMember', {
                                      shopId: $api.getStorage('user').data.shop_code
                                  })
                                },2000)
                              }
                            }
                        })
                },
                // 下载文件
                upFile: function(url, cb) {
                    var num = Math.random() + '';
                    var list = num.split('.');
                    api.download({
                        url: url,
                        cache: true,
                        savePath: 'fs://DCIM/Camera/' + list[1] + '.png',
                        allowResume: true
                    }, function(ret, err) {
                        if (ret.state == 1) {
                            api.saveMediaToAlbum({
                                path: 'fs://DCIM/Camera/' + list[1] + '.png'
                            }, function(ret, err) {
                                if (ret && ret.status) {
                                    cb('fs://DCIM/Camera/' + list[1] + '.png')
                                }
                            })
                        }
                        if (ret.state == 2) {
                            api.toast({
                                msg: ret.msg,
                                duration: 2000,
                                location: 'bottom'
                            })
                        }
                    });
                },
                //--我要开店
                goOpenShop: function(et) {
                    var that = this;
                    if (this.is_shop == 1) {
                        this.shop_get();
                        // api.openWin({
                        //     name: 'my_shop',
                        //     url: '../my_shop/my_shop.html',
                        //     reload: true
                        // });
                    } else {
                      if(!$api.getStorage('user').data.higher.out_user_id){
                        api.sendEvent({
                            name: 'openDialog'
                        });
                      }else{
                        api.openFrame({
                            name: 'openShop_fm',
                            url: './openShop_fm.html',
                            rect: {
                                x: 0,
                                y: 0,
                                w: api.winWidth,
                                h: api.winHeight
                            },
                            bounces: false,
                            bgColor: 'rgba(0,0,0,.3)'
                        });
                      }

                    }
                },
                //--我的订单，具体
                goMyorderList: function(et) {
                    var el = et.currentTarget;
                    var index = +el.dataset.index;
                    var name = '';
                    if (index == 4) {
                        $app.openWin("buyOrderAfter");
                    } else {
                        $app.openWin("buyOrderMy", {
                            index: index + 1
                        });
                    }
                },
                // 刷新
                execPullDown: function() {
                    var that = this;
                    console.log('111');
                    if ($api.getStorage('user')) {
                        that.getList();
                        // $app.getUserInfo($api.getStorage('user').data.openid, function(res) {
                        //
                        //
                        // })
                    }
                },
                // 前往找合作
                toHzuo:function(){
                  api.openWin({
                      name: 'submit',
                      url: './submit.html'
                  });

                },
                // 获取提示红点
                getShopRedTip:function(){
                  var that = this
                  if($api.getStorage('user').data.shop_code==''){
                      return
                  }
                    $app.ajax(
                        service.getShopRedTip, {
                            data: {
                                values: {
                                    shop_code:$api.getStorage('user').data.shop_code,
                                    mask:"none"
                                }
                            }
                        },
                        function(res) {
                            if (res.code==0) {
                              if(res.data.order_tip==1||res.data.comment_tip==1){
                                that.ShopRedTip = true
                              }else{
                                that.ShopRedTip = false
                              }
                              api.sendEvent({
                                  name: 'shopRed',
                                  extra:{
                                    ShopRedTip:res.data
                                  }
                              });
                            }
                        })
                },
            },
            mounted: function() {
                var self = this;

                //--前往个人设置页面（退出登陆）
                api.addEventListener({
                    name: 'login_flag'
                }, function(ret) {
                    self.intoShop(function() {
                        self.init()
                    });
                });
                //--订单返回刷新
                api.addEventListener({
                    name: 'orderBackperson_'
                }, function() {
                    self.init();
                });
                //--开店返回刷新
                api.addEventListener({
                    name: 'closeOpenShop'
                }, function() {
                    self.init();
                });
                api.addEventListener({
                    name: 'getShopRedTip'
                }, function(ret, err){
                    self.getShopRedTip()
                });

                api.addEventListener({
                    name: 'set__'
                }, function(ret, err) {
                    self.execPullDown();
                });
                self.execPullDown();

                api.setCustomRefreshHeaderInfo({
                    bgColor: '#fff',
                    images: [
                      'widget://image/refresh/new1.png',
                      'widget://image/refresh/new2.png',
                      'widget://image/refresh/new3.png',
                      'widget://image/refresh/new4.png',
                      'widget://image/refresh/new5.png',
                      'widget://image/refresh/new6.png',
                      'widget://image/refresh/new7.png',
                      'widget://image/refresh/new8.png',
                      'widget://image/refresh/new9.png',
                      'widget://image/refresh/new10.png',
                      'widget://image/refresh/new11.png',
                      'widget://image/refresh/new12.png',
                      'widget://image/refresh/new13.png',
                      'widget://image/refresh/new14.png',
                      'widget://image/refresh/new15.png',
                      'widget://image/refresh/new16.png',
                      'widget://image/refresh/new17.png',
                      'widget://image/refresh/new18.png',
                      'widget://image/refresh/new19.png',
                      'widget://image/refresh/new20.png',
                      'widget://image/refresh/new21.png',
                      'widget://image/refresh/new22.png',
                      'widget://image/refresh/new23.png',
                      'widget://image/refresh/new24.png',
                      'widget://image/refresh/new25.png'
                    ]
                }, function() {
                    //下拉刷新被触发，自动进入加载状态，使用 api.refreshHeaderLoadDone() 手动结束加载中状态
                    //下拉刷新被触发，使用 api.refreshHeaderLoadDone() 结束加载中状态
                    //alert('开始加载刷新数据，摇一摇停止加载状态');
                    self.intoShop(function() {
                        self.init()
                    });
                    api.refreshHeaderLoadDone()
                });
                setTimeout(function(){
                  api.parseTapmode()
                },6000)
            },
            created: function() {
                var that = this;
                this.intoShop(function() {
                    that.init()
                });
                //this.openShopMainShow();
            },

        };
        var even = function() {
            var that = this;
            $('.header-btn').on('click', function() {
                api.closeToWin({
                    name: 'root',
                    animation: {
                        type: 'fade',
                        subType: 'from_right',
                        duration: 300
                    }
                });
            });
            NVTabBar = api.require('NVTabBar');
            api.setStatusBarStyle({
                style: 'dark',
                color: '#DASFJHAJKS112222DFHKJASHFKJSAHDFJK'
            })

            $app.BACK();


        };
        var newVue = new Vue(vm);
        even();

    })
}
